package kontroler;

import interfejs.AddVehicleFrame;
import interfejs.VehiclesPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import viewModel.VehiclesTableModel;

import Model.Baza;
import Model.Samochod;
import Model.Wlasciciel;





public class VehiclesPanelController extends Controller implements ActionListener, TableModelListener, ListSelectionListener {

	private static VehiclesPanel pannel;
	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void tableChanged(TableModelEvent e) {
		// TODO Auto-generated method stub
		if(e.getType() == TableModelEvent.UPDATE){
			System.out.println("Poszedl UPDATE");
			int row = e.getFirstRow();
	        int column = e.getColumn();
	        VehiclesTableModel model = (VehiclesTableModel)e.getSource();
	        if(row < 0 || column < 0)
	        	return;
	        Object data = model.getValueAt(row, column);
	        Object id = model.getValueAt(row, 0);
	        Samochod tempVehicle = Baza.getPojazdyStatic().getById(id.toString());
	        
	        switch(column){
		        case 2:
		        	tempVehicle.setMarka((String) data);
		        break;
		        case 3:
		        	tempVehicle.setModel((String) data);
		        break;
		        case 4:
		        	tempVehicle.setRok_produkcji(Integer.parseInt((String)data));
		        break;
		        case 5:
		        	tempVehicle.setPojemnosc(Integer.parseInt((String)data));
		        break;
		        case 6:
		        	tempVehicle.setLiczba_miejsc(Integer.parseInt((String)data));
		        break;
		       
	        }
	        System.out.println(tempVehicle);
	        
	        //model.fireTableDataChanged();
	        System.out.println(row + " " + column+" "+ data +" " + id);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("NEW_VEHICLE")){
			pannel = ((VehiclesPanel)((JToolBar)((JButton) e.getSource()).getParent()).getParent());
			pannel.setAddVehicleFrame(new AddVehicleFrame());
			pannel.getAddVehicleFrame().createGUI();
			
		} else if(e.getActionCommand().equals("ADD_VEHICLE")){
			AddVehicleFrame frame = (AddVehicleFrame) ((JButton) e.getSource()).getTopLevelAncestor();
			Wlasciciel temp_w = (Wlasciciel) frame.getOwnersComboBox().getSelectedItem();
			Baza.getPojazdyStatic().addVehicle(temp_w, frame.getMark().getText(), frame.getModel().getText(), Integer.parseInt(frame.getYear().getText()), Integer.parseInt(frame.getCapacity().getText()), Integer.parseInt(frame.getNumSeats().getText()));
			if(pannel != null){
				pannel.getVehiclesTableModel().setTable(Baza.getPojazdyStatic());
				pannel.getVehiclesTable().repaint();
				pannel = null;
			}
			
			frame.dispose();
			
		}
		
	}

}
